package com.ml.mall.service.impl;

import com.ml.mall.bean.page.Page;
import com.ml.mall.bean.req.BaseDateRangeReq;
import com.ml.mall.bean.req.admin.category.AdminCategoryQueryPageReq;
import com.ml.mall.bean.req.admin.purchase.PurchaseQueryOneReq;
import com.ml.mall.bean.req.admin.variant.VariantQueryOneReq;
import com.ml.mall.bean.req.purchase.PurchaseCategoryQueryPageReq;
import com.ml.mall.bean.req.purchase.PurchaseSupplierQueryPageReq;
import com.ml.mall.bean.req.purchase.PurchaseVariantQueryPageReq;
import com.ml.mall.bean.res.admin.PurchaseQueryOneRes;
import com.ml.mall.bean.res.admin.PurchaseReportPageRes;
import com.ml.mall.bean.res.admin.category.AdminCategoryQueryReportPageRes;
import com.ml.mall.bean.res.admin.variant.VariantQueryOneRes;
import com.ml.mall.bean.res.admin.variant.VariantQueryTopRes;
import com.ml.mall.bean.res.admin.variant.vo.VariantQueryTopVO;
import com.ml.mall.bean.res.admin.vo.AdminOverviewSalesByCategoryVO;
import com.ml.mall.bean.res.purchase.PurchaseOverviewRes;
import com.ml.mall.bean.res.purchase.PurchaseSpentQueryReportRes;
import com.ml.mall.bean.res.purchase.PurchaseSupplierQueryReportRes;
import com.ml.mall.bean.res.purchase.PurchaseVariantQueryPageRes;
import com.ml.mall.bean.res.purchase.vo.PurchaseSupplierVO;
import com.ml.mall.bean.res.purchase.vo.PurchaseVariantVO;
import com.ml.mall.bean.res.supplier.SupplierVariantQueryPageRes;
import com.ml.mall.bean.vo.DateValueVO;
import com.ml.mall.consts.CommonConsts;
import com.ml.mall.consts.OverviewTopConsts;
import com.ml.mall.consts.UserRoleTypeConsts;
import com.ml.mall.conversion.AnalyticsAdminServiceHelp;
import com.ml.mall.conversion.AnalyticsPurchaseServiceHelp;
import com.ml.mall.entity.sys.SysUser;
import com.ml.mall.security.JwtUtil;
import com.ml.mall.service.AnalyticsAdminService;
import com.ml.mall.service.AnalyticsPartnersService;
import com.ml.mall.service.AnalyticsPurchaseService;
import com.ml.mall.service.AnalyticsSupplierService;
import com.ml.mall.service.asyn.AnalyticsAdminServiceAsyn;
import com.ml.mall.service.asyn.AnalyticsPurchaseServiceAsyn;
import com.ml.mall.service.asyn.AnalyticsSupplierServiceAsyn;
import com.ml.mall.utils.DateTransformUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * Created by kyyang on 2021/7/4 12:24
 */
@Service
public class AnalyticsPurchaseServiceImpl implements AnalyticsPurchaseService {

    @Autowired
    private AnalyticsPurchaseServiceAsyn analyticsPurchaseServiceAsyn;
    @Autowired
    private AnalyticsAdminServiceAsyn analyticsAdminServiceAsyn;
    @Autowired
    private AnalyticsAdminService analyticsAdminService;


    @Override
    public PurchaseOverviewRes getPurchaseOverview(BaseDateRangeReq req) throws Exception{
        SysUser currentUser = JwtUtil.getCurrentUser();
        if (currentUser != null && UserRoleTypeConsts.ROLE_TYPE_4 == currentUser.getRoleType()) {
            //按照分类 top N
            Future<List<AdminOverviewSalesByCategoryVO>> adminOverviewSalesByCategoryVOFuture = analyticsAdminServiceAsyn.selectSalesByCategoryTopN(OverviewTopConsts.ADMIN_OVERVIEW_STATE_TOP_N,req.getTimeBegin(),req.getTimeEnd(), null,currentUser.getCompanyId());
            //按照卖家 top N
            Future<List<PurchaseSupplierVO>> purchaseSupplierVOFuture = analyticsPurchaseServiceAsyn.selectSalesBySupplierTopN(OverviewTopConsts.ADMIN_OVERVIEW_STATE_TOP_N,req.getTimeBegin(),req.getTimeEnd(), currentUser.getCompanyId());
            //按照sku top N
            Future<List<PurchaseVariantVO>> purchaseVariantVOFuture = analyticsPurchaseServiceAsyn.selectSalesByVariantTopN(OverviewTopConsts.ADMIN_OVERVIEW_STATE_TOP_N,req.getTimeBegin(),req.getTimeEnd(), currentUser.getCompanyId());

            PurchaseOverviewRes purchaseOverviewConversion = AnalyticsPurchaseServiceHelp.getPurchaseOverviewConversion(adminOverviewSalesByCategoryVOFuture, purchaseSupplierVOFuture, purchaseVariantVOFuture);
            return purchaseOverviewConversion;
        }
        return null;
    }

    @Override
    public PurchaseSpentQueryReportRes getPurchaseSpentQueryReport(BaseDateRangeReq req) throws Exception {
        SysUser currentUser = JwtUtil.getCurrentUser();
        if(currentUser.getRoleType()== CommonConsts.ROLE_TYPE_ADMIN.intValue()){
            //查询订单报告
            PurchaseQueryOneRes purchaseQueryOneRes = purchaseOrderQueryReport(req, null);
            //按照分类 top N
            Future<List<DateValueVO>> purchaseVariantVOFuture = analyticsPurchaseServiceAsyn.getPurchaseSpentQueryReportByCategoryTopN(OverviewTopConsts.ADMIN_OVERVIEW_STATE_TOP_N,req.getTimeBegin(),req.getTimeEnd(), null);
            return AnalyticsPurchaseServiceHelp.getPurchaseSpentQueryReportConversion(purchaseQueryOneRes,purchaseVariantVOFuture,req);
        }else {
            //查询订单报告
            PurchaseQueryOneRes purchaseQueryOneRes = purchaseOrderQueryReport(req, currentUser.getCompanyId());
            //按照分类 top N
            Future<List<DateValueVO>> purchaseVariantVOFuture = analyticsPurchaseServiceAsyn.getPurchaseSpentQueryReportByCategoryTopN(OverviewTopConsts.ADMIN_OVERVIEW_STATE_TOP_N,req.getTimeBegin(),req.getTimeEnd(), currentUser.getCompanyId());
            System.out.print(AnalyticsPurchaseServiceHelp.getPurchaseSpentQueryReportConversion(purchaseQueryOneRes,purchaseVariantVOFuture,req));
            return AnalyticsPurchaseServiceHelp.getPurchaseSpentQueryReportConversion(purchaseQueryOneRes,purchaseVariantVOFuture,req);

        }
    }

    @Override
    public List<PurchaseSupplierQueryReportRes> getPurchaseSupplierQueryReport(BaseDateRangeReq req) throws Exception {
        SysUser currentUser = JwtUtil.getCurrentUser();
        if (currentUser != null && UserRoleTypeConsts.ROLE_TYPE_4 == currentUser.getRoleType()) {
            Future<List<DateValueVO>> purchaseSupplierVOFuture = analyticsPurchaseServiceAsyn.getPurchaseSupplierQueryReport(OverviewTopConsts.ADMIN_OVERVIEW_STATE_TOP_N, req.getTimeBegin(), req.getTimeEnd(), currentUser.getCompanyId());
            return AnalyticsPurchaseServiceHelp.getPurchaseSupplierQueryReportConversion(purchaseSupplierVOFuture,req);
        }else {
            Future<List<DateValueVO>> purchaseSupplierVOFuture = analyticsPurchaseServiceAsyn.getPurchaseSupplierQueryReport(OverviewTopConsts.ADMIN_OVERVIEW_STATE_TOP_N, req.getTimeBegin(), req.getTimeEnd(), null);
            return AnalyticsPurchaseServiceHelp.getPurchaseSupplierQueryReportConversion(purchaseSupplierVOFuture,req);
        }
    }

    @Override
    public Page<PurchaseSupplierVO> getPurchaseSupplierQueryPage(PurchaseSupplierQueryPageReq req) throws Exception {
        SysUser currentUser = JwtUtil.getCurrentUser();
        Page<PurchaseSupplierVO> res = new Page<PurchaseSupplierVO>(req.getPageIndex(), req.getPageNum());
        int count = 0;
        Future<Integer> countFuture=null;
        Long companyId=null;
        if (currentUser != null && UserRoleTypeConsts.ROLE_TYPE_1 == currentUser.getRoleType()) {
            countFuture = analyticsPurchaseServiceAsyn.selectPurchaseSupplierQueryPageCount(req.getTimeBegin(), req.getTimeEnd(), req.getSortType(), req.getSupplierCode(), companyId);
        }else {
            companyId=currentUser.getCompanyId();
            countFuture = analyticsPurchaseServiceAsyn.selectPurchaseSupplierQueryPageCount(req.getTimeBegin(), req.getTimeEnd(), req.getSortType(), req.getSupplierCode(), companyId);
        }
        if (countFuture != null) {
            count = countFuture.get();
        }
        res.setTotal(count);
        if (count > 0) {
            Future<List<PurchaseSupplierVO>> purchaseReportPageResFuture = analyticsPurchaseServiceAsyn.selectPurchaseSupplierQueryPageList(req.getTimeBegin(), req.getTimeEnd(), req.getSortType(), req.getSupplierCode(), req.getStart(), req.getEnd(), companyId);
            if (purchaseReportPageResFuture != null) {
                List<PurchaseSupplierVO> purchaseReportPageRes = purchaseReportPageResFuture.get();
                res.setRecords(purchaseReportPageRes);
            }
        }
        return res;
    }
    @Override
    public Page<SupplierVariantQueryPageRes> purchaseCategoryQueryPage(PurchaseCategoryQueryPageReq req) throws ExecutionException, InterruptedException {
        SysUser sysUser = JwtUtil.getCurrentUser();
        Page<SupplierVariantQueryPageRes> res = new Page<SupplierVariantQueryPageRes>(req.getPageIndex(),req.getPageNum());
        //获取数量
        int count = 0;
        Future<Integer> countFuture = analyticsPurchaseServiceAsyn.selectPurchaseCategoryPageCount(req.getTimeBegin(), req.getTimeEnd(),sysUser.getCompanyId(),req.getCategoryName());
        if (countFuture != null && countFuture.get() != null) {
            count = countFuture.get();
        }
        //获取数据
        res.setTotal(count);
        if (count > 0) {
            Future<List<SupplierVariantQueryPageRes>> purchaseReportPageResFuture = analyticsPurchaseServiceAsyn.selectPurchaseCategoryPageList(req.getTimeBegin(), req.getTimeEnd(), req.getSortType(), sysUser.getCompanyId(),req.getCategoryName(), req.getStart(), req.getEnd());
            if (purchaseReportPageResFuture != null) {
                List<SupplierVariantQueryPageRes> purchaseReportPageRes = purchaseReportPageResFuture.get();
                res.setRecords(purchaseReportPageRes);
            }
        }
        return res;
    }

    @Override
    public Page<PurchaseVariantQueryPageRes> purchaseVariantQueryPage(PurchaseVariantQueryPageReq req) throws Exception {
        SysUser currentUser = JwtUtil.getCurrentUser();
        if (currentUser != null && UserRoleTypeConsts.ROLE_TYPE_4 == currentUser.getRoleType()) {
            Page<PurchaseVariantQueryPageRes> res = new Page<PurchaseVariantQueryPageRes>(req.getPageIndex(), req.getPageNum());
            int count = 0;
            Future<Integer> countFuture = analyticsPurchaseServiceAsyn.selectPurchaseVariantQueryPageCount(req.getTimeBegin(), req.getTimeEnd(),  req.getSupplierCode(), currentUser.getCompanyId(),req.getCategoryId(),req.getVariantName());
            if (countFuture != null) {
                count = countFuture.get();
            }
            res.setTotal(count);
            if (count > 0) {
                Future<List<PurchaseVariantQueryPageRes>> purchaseReportPageResFuture = analyticsPurchaseServiceAsyn.selectPurchaseVariantQueryPageList(req.getTimeBegin(), req.getTimeEnd(), req.getSortType(), req.getSupplierCode(), req.getStart(), req.getEnd(), currentUser.getCompanyId(),req.getCategoryId(),req.getVariantName());
                if (purchaseReportPageResFuture != null) {
                    List<PurchaseVariantQueryPageRes> purchaseReportPageRes = purchaseReportPageResFuture.get();
                    res.setRecords(purchaseReportPageRes);
                }
            }
            return res;
        }
        return null;
    }

    @Override
    public List<VariantQueryTopRes> purchaseVariantQueryTop(BaseDateRangeReq req) throws Exception {
        SysUser currentUser = JwtUtil.getCurrentUser();
        if (currentUser != null && UserRoleTypeConsts.ROLE_TYPE_4 == currentUser.getRoleType()) {
            Future<List<VariantQueryTopRes>> variantQueryTopFuture=analyticsPurchaseServiceAsyn.selectPurchaseVariantQueryTop(req,OverviewTopConsts.ADMIN_OVERVIEW_STATE_TOP_N,currentUser.getCompanyId());
            if(variantQueryTopFuture!=null){
                List<VariantQueryTopRes> variantQueryTopRes = variantQueryTopFuture.get();
                for(VariantQueryTopRes res:variantQueryTopRes){
                    //List<VariantQueryTopVO> list = analyticsAdminServiceAsyn.selectAdminVariantQueryOneByTimeLine(req.getTimeBegin(),req.getTimeEnd(),res.getVariantId(),null);
                    List<VariantQueryTopVO> list = analyticsPurchaseServiceAsyn.selectPurchaseVariantQueryOneByTimeLine(req.getTimeBegin(),req.getTimeEnd(),res.getVariantId(),currentUser.getCompanyId());
                    res.setDayDetail(DateTransformUtils.getCurDateMonthDataInit(req.getTimeBegin(),req.getTimeEnd(),list));
                }
                return variantQueryTopRes;
            }
        }
        return Collections.emptyList();
    }

    @Override
    public VariantQueryOneRes purchaseVariantQueryOne(VariantQueryOneReq req)throws Exception {
        SysUser currentUser = JwtUtil.getCurrentUser();
        if (currentUser != null && UserRoleTypeConsts.ROLE_TYPE_4 == currentUser.getRoleType()) {
            VariantQueryOneRes adminCategoryQueryReportPageResPage = analyticsAdminService.adminVariantQueryOne(req, null);
            return adminCategoryQueryReportPageResPage;
        }
        return null;
    }

    @Override
    public PurchaseQueryOneRes purchaseOrderQueryReport(BaseDateRangeReq reqt, Long supplierId) throws Exception {
        SysUser currentUser = JwtUtil.getCurrentUser();
        if(currentUser.getRoleType()== CommonConsts.ROLE_TYPE_ADMIN.intValue()){
            //查询时间范围内 买家订单 订单数
            Future<List<DateValueVO>> purchaseOrderArrFuture = analyticsAdminServiceAsyn.selectPurchaseOrderByTimeLine(reqt.getTimeBegin(), reqt.getTimeEnd(), null, supplierId);
            //查询时间范围内 买家订单花费金额
            Future<List<DateValueVO>> purchaseSpentArrFuture = analyticsAdminServiceAsyn.selectPurchaseSpentByTimeLine(reqt.getTimeBegin(), reqt.getTimeEnd(), null, supplierId);
            PurchaseQueryOneReq req = new PurchaseQueryOneReq();
            req.setTimeBegin(reqt.getTimeBegin());
            req.setTimeEnd(reqt.getTimeEnd());
            return AnalyticsAdminServiceHelp.adminPurchaseQueryOneDetailConversion(req, purchaseOrderArrFuture, purchaseSpentArrFuture);
        }else {
            //查询时间范围内 买家订单 订单数
            Future<List<DateValueVO>> purchaseOrderArrFuture = analyticsAdminServiceAsyn.selectPurchaseOrderByTimeLine(reqt.getTimeBegin(), reqt.getTimeEnd(), currentUser.getCompanyId(), null);
            //查询时间范围内 买家订单花费金额
            Future<List<DateValueVO>> purchaseSpentArrFuture = analyticsAdminServiceAsyn.selectPurchaseSpentByTimeLine(reqt.getTimeBegin(), reqt.getTimeEnd(), currentUser.getCompanyId(), null);
            PurchaseQueryOneReq req = new PurchaseQueryOneReq();
            req.setTimeBegin(reqt.getTimeBegin());
            req.setTimeEnd(reqt.getTimeEnd());
            return AnalyticsAdminServiceHelp.adminPurchaseQueryOneDetailConversion(req, purchaseOrderArrFuture, purchaseSpentArrFuture);
        }

    }

    @Override
    public Page<AdminCategoryQueryReportPageRes> purchaseCategoryQueryReportPage(AdminCategoryQueryPageReq req, Long companyId) throws Exception{
        Page<AdminCategoryQueryReportPageRes> res = new Page<>(req.getPageIndex(),req.getPageNum());
        int count = 0;
        Future<Integer> countFuture = analyticsPurchaseServiceAsyn.selectPurchaseCategoryQueryReportPageCount(req.getTimeBegin(),req.getTimeEnd(),req.getCategoryName(),req.getIndustryId(),companyId);
        if(countFuture!=null){
            count =  countFuture.get();
        }
        res.setTotal(count);
        if(count>0){
            Future<List<AdminCategoryQueryReportPageRes>> purchaseReportPageResFuture = analyticsPurchaseServiceAsyn.selectPurchaseCategoryQueryReportPageList(req.getTimeBegin(),req.getTimeEnd(),req.getSortType(),req.getCategoryName(),req.getIndustryId(),req.getStart(),req.getEnd(),companyId);
            if(purchaseReportPageResFuture!=null){
                List<AdminCategoryQueryReportPageRes> purchaseReportPageRes = purchaseReportPageResFuture.get();
                res.setRecords(purchaseReportPageRes);
            }
        }
        return res;
    }
}
